//
//  SystemMessageViewController.m
//  MagictoHire
//
//  Created by 锋 on 2017/12/22.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "SystemMessageViewController.h"
#import "SystemMessageTableViewCell.h"
#import "SystemMessageModel.h"
#import "FirstProduceLineDetailViewController.h"
#import "JobDetailViewController.h"

@interface SystemMessageViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *systemMessageTableView;
@property (nonatomic, strong) NSArray *systemMessageArr;
@end

@implementation SystemMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupSystemMessageTableView];
    [self requsetSystemMessage];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requsetSystemMessage];
}

- (void)setupUI {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"系统消息";
    self.view.backgroundColor = RGB(242, 242, 242);
}
- (void)setupSystemMessageTableView {
    _systemMessageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) style:UITableViewStyleGrouped];
    _systemMessageTableView.delegate = self;
    _systemMessageTableView.dataSource = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
    view.backgroundColor = [UIColor whiteColor];
    _systemMessageTableView.tableHeaderView = view;
    _systemMessageTableView.tableFooterView = view;
    _systemMessageTableView.backgroundColor = RGB(242, 242, 242);
    _systemMessageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_systemMessageTableView];
}

- (void)requsetSystemMessage {
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestMessageMineParmers:nil Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             _systemMessageArr = [SystemMessageModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
             WEAKSELF;
             dispatch_async(dispatch_get_main_queue(), ^{
                 [weakSelf.systemMessageTableView reloadData];
             });
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

// MARK:tableview的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _systemMessageArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    SystemMessageModel *model = _systemMessageArr[indexPath.row];
    if ([model.status isEqualToString:@"0"]) {
        if ([model.type isEqualToString:@"1"] || [model.type isEqualToString:@"5"] || [model.type isEqualToString:@"6"]) {
            return 153;
        } else {
            return 195;
        }
    } else {
        return 153;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SystemMessageTableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:@"SystemMessageTableViewCell" owner:self options:nil].lastObject;
    SystemMessageModel *model = _systemMessageArr[indexPath.row];
    if ([model.status isEqualToString:@"0"]) {
        if ([model.type isEqualToString:@"1"]) {
            cell.rejectBtn.hidden = YES;
            cell.acceptBtn.hidden = YES;
        } else if ([model.type isEqualToString:@"2"] || [model.type isEqualToString:@"3"] || [model.type isEqualToString:@"4"]) {
            cell.contentLabel.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkProjectDetail:)];
            cell.contentLabel.tag = indexPath.row;
            [cell.contentLabel addGestureRecognizer:tap];
        } else if ([model.type isEqualToString:@"5"] || [model.type isEqualToString:@"6"]) {
            cell.rejectBtn.hidden = YES;
            cell.acceptBtn.hidden = YES;
            cell.contentLabel.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkProjectDetail:)];
            cell.contentLabel.tag = indexPath.row;
            [cell.contentLabel addGestureRecognizer:tap];
        }
    } else {
        if ([model.type isEqualToString:@"2"] || [model.type isEqualToString:@"3"] || [model.type isEqualToString:@"4"] || [model.type isEqualToString:@"5"] || [model.type isEqualToString:@"6"]) {
            cell.contentLabel.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkProjectDetail:)];
            cell.contentLabel.tag = indexPath.row;
            [cell.contentLabel addGestureRecognizer:tap];
        }
        cell.rejectBtn.hidden = YES;
        cell.acceptBtn.hidden = YES;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentLabel.text = model.content;
    cell.dateLabel.text = model.createDtm;
    cell.rejectBtn.layer.borderColor = [ThemeColor CGColor];
    cell.rejectBtn.layer.borderWidth = 1;
    cell.rejectBtn.tag = indexPath.row;
    [cell.rejectBtn addTarget:self action:@selector(onClickRejectBtn:) forControlEvents:UIControlEventTouchUpInside];
    cell.acceptBtn.tag = indexPath.row;
    [cell.acceptBtn addTarget:self action:@selector(onClickAcceptBtn:) forControlEvents:UIControlEventTouchUpInside];
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)checkProjectDetail:(UITapGestureRecognizer *)tap {
    SystemMessageModel *model = _systemMessageArr[tap.view.tag];
    if ([model.status isEqualToString:@"0"]) {
        if (![model.type isEqualToString:@"1"]) {
            if ([model.type isEqualToString:@"2"]) {
                FirstProduceLineDetailViewController *projectDetailVC = [[FirstProduceLineDetailViewController alloc] init];
                projectDetailVC.ID = model.joinId;
                projectDetailVC.isMessage = NO;
                [self.navigationController pushViewController:projectDetailVC animated:YES];
            } else if ([model.type isEqualToString:@"5"]) {
                JobDetailViewController *vc = [[JobDetailViewController alloc] init];
                vc.ID = model.joinId;
                vc.statusNum = 4;
                vc.detailType = RECOMMENDTYPE;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    } else {
        if ([model.type isEqualToString:@"2"]) {
            FirstProduceLineDetailViewController *projectDetailVC = [[FirstProduceLineDetailViewController alloc] init];
            projectDetailVC.ID = model.joinId;
            projectDetailVC.isMessage = YES;
            [self.navigationController pushViewController:projectDetailVC animated:YES];
        } else if ([model.type isEqualToString:@"5"]) {
            JobDetailViewController *vc = [[JobDetailViewController alloc] init];
            vc.ID = model.joinId;
            vc.statusNum = 1;
            vc.detailType = RECOMMENDTYPE;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            JobDetailViewController *vc = [[JobDetailViewController alloc] init];
            vc.ID = model.joinId;
            vc.detailType = EVALUATETYPE;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (void)onClickAcceptBtn:(UIButton *)sender {
    SystemMessageModel *model = _systemMessageArr[sender.tag];
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    if ([model.status isEqualToString:@"0"]) {
        if ([model.type isEqualToString:@"2"]) {
            [HYBaseHandler requestQuotationAcceptParmers:@{@"id" : model.joinId} Success:^(id obj)
             {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 if ([obj server_Getcodecheck])
                 {
                     NSLog(@"%@", obj);
                     [LTools showMBProgressWithText:@"接受成功" addToView:weakSelf.view];
                     [self requsetSystemMessage];
                 }
                 else
                 {
                     [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
                 }
             } failed:^(NSError *error) {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 
             }];
        } else if ([model.type isEqualToString:@"3"]) {
            [HYBaseHandler requestQuotationAcceptSecondParmers:@{@"id" : model.joinId} Success:^(id obj)
             {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 if ([obj server_Getcodecheck])
                 {
                     NSLog(@"%@", obj);
                     [LTools showMBProgressWithText:@"接受成功" addToView:weakSelf.view];
                     [self requsetSystemMessage];
                 }
                 else
                 {
                     [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
                 }
             } failed:^(NSError *error) {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 
             }];
        } else if ([model.type isEqualToString:@"4"]) {
            [HYBaseHandler requestQuotationAcceptThirdParmers:@{@"id" : model.joinId} Success:^(id obj)
             {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 if ([obj server_Getcodecheck])
                 {
                     NSLog(@"%@", obj);
                     [LTools showMBProgressWithText:@"接受成功" addToView:weakSelf.view];
                     [self requsetSystemMessage];
                 }
                 else
                 {
                     [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
                 }
             } failed:^(NSError *error) {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 
             }];
        }
    }
    
}

- (void)onClickRejectBtn:(UIButton *)sender {
    SystemMessageModel *model = _systemMessageArr[sender.tag];
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    if ([model.status isEqualToString:@"0"]) {
        if ([model.type isEqualToString:@"2"]) {
            [HYBaseHandler requestQuotationRefuseParmers:@{@"id" : model.joinId} Success:^(id obj)
             {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 if ([obj server_Getcodecheck])
                 {
                     NSLog(@"%@", obj);
                     [LTools showMBProgressWithText:@"拒绝成功" addToView:weakSelf.view];
                     [self requsetSystemMessage];
                 }
                 else
                 {
                     [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
                 }
             } failed:^(NSError *error) {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 
             }];
        } else if ([model.type isEqualToString:@"3"]) {
            [HYBaseHandler requestQuotationRefuseSecondParmers:@{@"id" : model.joinId} Success:^(id obj)
             {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 if ([obj server_Getcodecheck])
                 {
                     NSLog(@"%@", obj);
                     [LTools showMBProgressWithText:@"拒绝成功" addToView:weakSelf.view];
                     [self requsetSystemMessage];
                 }
                 else
                 {
                     [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
                 }
             } failed:^(NSError *error) {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 
             }];
        } else if ([model.type isEqualToString:@"4"]) {
            [HYBaseHandler requestQuotationRefuseThirdParmers:@{@"id" : model.joinId} Success:^(id obj)
             {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 if ([obj server_Getcodecheck])
                 {
                     NSLog(@"%@", obj);
                     [LTools showMBProgressWithText:@"拒绝成功" addToView:weakSelf.view];
                     [self requsetSystemMessage];
                 }
                 else
                 {
                     [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
                 }
             } failed:^(NSError *error) {
                 [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
                 
             }];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
